home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7254 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Reading directory into array of some kind.
  5. Date: 12 Feb 1996 16:42:12 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824160386@pegasus.montclair.edu>
  8. References: <4fis2r$tc7@news1.sunbelt.net>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. dking@SunBelt.Net writes:
  13.  
  14. >The following bit of code uses the findfirst/findnext to read all the 
  15. >filesname out of the current directory. I had HOPED to be able to put the file 
  16. >names into some sort of array but couldn't find a way to make it work. 
  17.  
  18.    _dos_findfirst() and _dos_findnext() aren't specified in ANSI C.  In fact,
  19. they're not even that portable across PC platforms.  That being said,
  20.  
  21. >Any help on this would be greatly appreciated. 
  22.  
  23.    ... I don't see any reason not to help anyway.  :)  The point of interest
  24. is that the ffblk structure returned by findfirst() (and its brethren, but
  25. findfirst() is at least more PC-portable) includes a char name[13] member.
  26. When we leave off the []'s, name devolves into a pointer to a char (name[0]).
  27. So what we have for lack of technical specificity, is a "string."  To work
  28. with strings, you should include the standard <string.h> header in your file.
  29. Here are my suggestions, the extension of saving these names into your array
  30. is straightforward.
  31.  
  32.  
  33. >#include <stdio.h>
  34. >#include <dos.h>
  35. :#include <string.h>
  36.  
  37. >int main()
  38. >{
  39. >  
  40. >  struct find_t ffblk;
  41. >  int done;
  42. :  char filename[13];
  43. >  printf("Directory listing \n");
  44.  
  45. >  done = _dos_findfirst("*.*",255,&ffblk); //reads all files from directory
  46. >  while (!done) 
  47. >      {
  48. :       strcpy(filename, ffblk.name);      // filename <- ffblk.name
  49. >    printf("  %s\n", ffblk.name);      //print file names
  50. >    done = _dos_findnext(&ffblk);
  51. >    }
  52.  
  53. > return 0;
  54. >}
  55.  
  56.    That should do it.  :)  Always know your standard C library better than
  57. your DOS-specific one.
  58.                                                      -- Stone
  59. --
  60. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  61. # - Computer Science Undergrad, Montclair State University, NJ
  62. # - My views are my own, nobody else is this creative.  3;)>
  63. ... Tadpoles turn into Frogs.  What do frogs turn into?
  64.